package group2.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import group2.model.Book;
import group2.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.List;

@Controller
@RequestMapping("/book")

public class BookController {

    @Autowired
    BookService bookService;

    /*
    * 书籍查询
    * *//*
    @RequestMapping("/selectAll")
    public String search(ModelMap modelMap, HttpSession session){

        List<Book> bookList = this.bookService.selectAll();

        session.setAttribute("bookList",bookList);

        return "redirect:/views/book-list.jsp";
    }*/
    /*
    * 书籍删除
    * */
    @RequestMapping("/delete")
    public String delectbyId(int id){
        bookService.delBookById(id);
        return "redirect:/book/selectAll";
    }

   /* *//*
    * 书籍增加
    * *//*
    @RequestMapping("/add")
    public String addBook(Book book){
        bookService.addBook(book);
        return "redirect:/book/selectAll";
    }*/
    /*
     * 书籍增加
     * 图片上传
     * */
    @RequestMapping("/add")
    public String addBook(Book book,HttpServletRequest request,
                          Model model,HttpSession session,
                            @RequestParam("images") MultipartFile file )throws Exception{
        bookService.addBook(book);
        //保存数据库的路径
        String sqlPath = null;
        //定义文件保存的本地路径
        String localPath="D:\\javaproject\\TaobookManagement\\src\\main\\webapp\\images\\";
        //定义 文件名
        String filename=null;
        if (!file.isEmpty()){
            //获得文件类型（可以判断如果不是图片，禁止上传）
            String contentType=file.getContentType();
            //获得文件后缀名
            String suffixName=contentType.substring(contentType.indexOf("/")+1);
            //得到 文件名
            filename=book.getImg()+"."+suffixName;
            //文件保存路径
            file.transferTo(new File(localPath+filename));
        }
        return "redirect:/book/list";
    }
    /*
    * 书籍修改
    * */

    /*
    * 书籍搜索（条件查询）
    * */
    @RequestMapping("/searchBook")
    public String searchBook(Book book, HttpSession session){
        //book.setBookName("炼丹术的故事");
        List<Book> searchBook=this.bookService.selectBook(book);
        session.setAttribute("searchBook",searchBook);
        return "redirect:/views/book-search.jsp";
    }

    /*
    * 书籍分页
    * */
    @RequestMapping("/list")
    public String search(@RequestParam(required= true, defaultValue="1") Integer page,
                         HttpServletRequest request, Model model, HttpSession session){
        //PageHelper.startPage(pageNum, pageSize);
        // 这段代码表示，程序开始分页了，pageNum默认值是1，pageSize默认是10，意思是从第1页开始，每页显示10条记录。
        PageHelper.startPage(page, 8);
        // 查询列表的结果
        List<Book> bookList = this.bookService.selectAll();
        // 得到分页的结果对象
        PageInfo<Book> pageList = new PageInfo<>(bookList);
        //model.addAttribute("page", pageList);
        session.setAttribute("page",pageList);

        return "redirect:/views/book-list.jsp";
    }

}
